﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
using System.IO;
namespace QuanLyDeTaiKHCN.UserControls.Manager
{
    public partial class PheDuyetDeTai : System.Web.UI.UserControl
    {
        public SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                var itemhoidong = _db.tblHopHoiDongs.Where(r => r.MaDeTai.Trim() == _ps.Decrypt(str, "gold", 128));
                if (itemhoidong.Count() == 0)
                {
                    lblMessage.Text = "Đề tài này chưa tổ chức họp hội đồng.";
                    lblMessage.Visible = true;
                    pncontent.Visible = false;
                }
                else if ((bool)itemhoidong.First().KetQua == false)
                {
                    lblMessage.Text = "Đề tài này không cho phép thực hiện.";
                    lblMessage.Visible = true;
                    pncontent.Visible = false;
                }
                else
                {
                    var item = _db.tblDanhSachDeTaiDuAns.Single(r => r.MaDeTaiDuAn.Trim() == _ps.Decrypt(str, "gold", 128));
                    int tongkinhphi = 0;
                    try
                    {
                        tongkinhphi = int.Parse(item.TongKinhPhi.ToString());
                    }
                    catch { }
                    if (tongkinhphi == 0)
                    {
                        lblMessage.Visible = true;
                        lblMessage.Text = "Đề tài này chưa được lập thuyết minh.";
                        pncontent.Visible = false;
                    }
                    else
                    {
                        bool thamquyenso;
                        if (item.LinhVuc == 1)
                        {
                            if (tongkinhphi <= 600000000)
                                thamquyenso = true;
                            else
                                thamquyenso = false;
                        }
                        else
                        {
                            if (tongkinhphi <= 300000000)
                                thamquyenso = true;
                            else
                                thamquyenso = false;
                        }
                        if (thamquyenso == true)
                        {
                            pnsototrinh.Enabled = false;
                            lblqd.Text = "QĐ-SKHCN";
                        }
                        else
                        {
                            pnsototrinh.Enabled = true;
                            lblqd.Text = "QĐ-UBND";
                        }
                        BindGrid(madetai);
                        BindData();
                    }
                }
            }
        }

        protected void BindData()
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            try
            {
                var item = _db.tblPheDuyetDeTais.Single(r => r.MaDeTai.Trim() == madetai);
                txtSoQuyetDinh.Text = item.SoQuyetDinh;
                txtNgayRaQuyetDinh.Text = string.Format("{0:dd/MM/yyyy}", item.NgayQD);
                txtToTrinhSo.Text = item.SoToTrinh;
                txtNgayTrinh.Text = string.Format("{0:dd/MM/yyyy}", item.NgayTrinh);
            }
            catch
            {
                txtSoQuyetDinh.Text = "";
                txtNgayRaQuyetDinh.Text = "";
                txtToTrinhSo.Text = "";
                txtNgayTrinh.Text = "";
            }
        }

        protected void LinkAddFile_Click(object sender, EventArgs e)
        {
            if (filebienban.HasFile)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                DateTime t = DateTime.Now;
                string filename = (t.Day < 10 ? "0" + t.Day.ToString() : t.Day.ToString()) + (t.Month < 10 ? "0" + t.Month.ToString() : t.Month.ToString()) + t.Year.ToString() + t.Hour.ToString() + t.Minute.ToString() + t.Second.ToString() + Path.GetExtension(filebienban.FileName);
                tblFile _tb = new tblFile();
                _tb.MaDeTaiDuAn = madetai;
                _tb.FileThuyetMinh = filename;
                _tb.GhiChuFileThuyetMinh = txtghichufilethuyetminh.Text;
                _tb.NgayNhanThuyetMinh = CatalogAccess.ConvertToDate(txtNgayNhan.Text);
                _tb.FileCuaBuoc = 5;
                try
                {
                    _db.tblFiles.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    BindGrid(madetai);
                    filebienban.SaveAs(Server.MapPath(PageURL.local + "filehoso/" + filename));
                    txtghichufilethuyetminh.Text = "";
                }
                catch { }
            }
        }

        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            var item = _db.tblFiles.Single(r => r.ID == int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblFiles.DeleteOnSubmit(item);
                _db.SubmitChanges();
                try
                {
                    string filename = ((Label)grid.Rows[e.RowIndex].FindControl("lblFileName")).Text;
                    System.IO.File.Delete(Server.MapPath(PageURL.local + "filehoso/" + filename));
                }
                catch { }
                BindGrid(madetai);
            }
            catch { }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            try
            {
                var _tb = _db.tblPheDuyetDeTais.Single(r => r.MaDeTai.Trim() == madetai);
                _tb.SoQuyetDinh = txtSoQuyetDinh.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayRaQuyetDinh.Text))
                {
                    _tb.NgayQD = CatalogAccess.ConvertToDate(txtNgayRaQuyetDinh.Text);
                }
                _tb.SoToTrinh = txtToTrinhSo.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayTrinh.Text))
                {
                    _tb.NgayTrinh = CatalogAccess.ConvertToDate(txtNgayTrinh.Text);
                }
                try
                {
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu";
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
            catch
            {
                tblPheDuyetDeTai _tb = new tblPheDuyetDeTai();
                _tb.MaDeTai = madetai;
                _tb.SoQuyetDinh = txtSoQuyetDinh.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayRaQuyetDinh.Text))
                {
                    _tb.NgayQD = CatalogAccess.ConvertToDate(txtNgayRaQuyetDinh.Text);
                }
                _tb.SoToTrinh = txtToTrinhSo.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayTrinh.Text))
                {
                    _tb.NgayTrinh = CatalogAccess.ConvertToDate(txtNgayTrinh.Text);
                }
                try
                {
                    _db.tblPheDuyetDeTais.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu";
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
        }
        protected void BindGrid(string madetai)
        {
            var list = from p in _db.tblFiles.Where(r => r.MaDeTaiDuAn.Trim() == madetai && r.FileCuaBuoc == 5)
                       select new
                       {
                           ID = p.ID,
                           TrichYeu = p.GhiChuFileThuyetMinh.Trim(),
                           NgayLuu = string.Format("{0:dd/MM/yyyy}", p.NgayNhanThuyetMinh),
                           FileThuyetMinh = p.FileThuyetMinh.Trim(),
                           //sau khi up len host thi xai link1:
                           //Fileonline =domain_name + "\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //test xaif link2
                           //Fileonline = "localhost\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //demo link3:
                           Fileonline = "http://thongkekhcnbd.khcnbinhduong.gov.vn/userfiles/vanban/9475707965_493_296.pdf"

                       };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }
    }
}